package com.medical.data.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.medical.data.component.LoginUserThreadLocal;
import com.medical.data.domain.DataCollector;
import com.medical.data.domain.Department;
import com.medical.data.domain.HospitalInformation;
import com.medical.data.domain.User;
import com.medical.data.exception.MyException;
import com.medical.data.mapper.DataCollectorMapper;
import com.medical.data.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.data.utils.ResultCode;
import com.medical.data.vo.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 基本信息 服务实现类
 * </p>
 *
 * @author cj
 * @since 2024-01-23
 */
@Service
public class DataCollectorServiceImpl extends ServiceImpl<DataCollectorMapper, DataCollector> implements DataCollectorService {

    @Autowired
    private DepartmentService departmentService;


    @Autowired
    private PatientService patientService;

    @Autowired
    private ProjectDataService projectDataService;

    @Autowired
    private FormDataService formDataService;

    @Autowired
    private FormTemplateService formTemplateService;

    @Autowired
    private TokenSerivce tokenSerivce;

    @Autowired
    private HospitalInformationService hospitalInformationService;

    @Autowired
    private FollowPatientService followPatientService;

    @Autowired
    private UserService userService;


    @Override
    public Map getData() {
        Map<String, Object> map = new HashMap<>();
        //项目总数
        int projectNumber = projectDataService.getProjectNumber();
        map.put("projectNumber", projectNumber);

        //患者总数
        int patientNumber = patientService.getPatientNumber();
        map.put("patientNumber", patientNumber);

        //表单模版总数，这里值统计模版广场的模版
        int formTemplateNumber = formTemplateService.getFormTemplateNumber();
        map.put("formTemplateNumber", patientNumber);

        //统计过去一星期的填写数据的次数
     /*   List<java.lang.Integer>list=new ArrayList<>();
        list=formDataService.passDataNumber();
        map.put("list",list);
*/
        return map;
    }

    @Override
    public Map<String, String> getFirstLine() {
        Map<String, String> map = new LinkedHashMap<>();
        User user = tokenSerivce.getUser();
        map.put("userName", user.getNickName());
        map.put("hospitalName", hospitalInformationService.getNameById(user.getHsId()));
        map.put("userNumber", String.valueOf(userService.getUserNumber()));
        map.put("todayUserNumber", String.valueOf(userService.getTodayUserNumber()));
        map.put("projectNumber", String.valueOf(projectDataService.getProjectNumber()));
        map.put("todayProjectNumber", String.valueOf(projectDataService.getTodayProjectNumber()));
        map.put("formTemplateNumber", String.valueOf(formTemplateService.getFormTemplateNumber()));
        map.put("todayFormTemplateNumber", String.valueOf(formTemplateService.getTodayFormTemplateNumber()));
        map.put("patientNumber", String.valueOf(patientService.getPatientNumber()));
        map.put("todayPatientNumber", String.valueOf(patientService.getTodayPatientNumber()));
        return map;
    }

    @Override
    public Map<String, String> getSecondLine() {
        Map<String, String> map = new LinkedHashMap<>();
        User user = tokenSerivce.getUser();
        map.put("userName", user.getNickName());
        map.put("hospitalName", hospitalInformationService.getNameById(user.getHsId()));
        map.put("followFinishNumber", String.valueOf(followPatientService.getFollowFinishNumber()));
        map.put("followTodayFinishNumber", String.valueOf(followPatientService.getTodayFollowFinishNumber()));
        map.put("followNoFinishNumber", String.valueOf(followPatientService.getFollowNoFinishNumber()));
        map.put("followNoTodayFinishNumber", String.valueOf(followPatientService.getTodayNoFollowFinishNumber()));
        map.put("formData", String.valueOf(formDataService.getFormDataNumber()));
        map.put("formDataToday", String.valueOf(formDataService.getTodayFormDataNumber()));
        return map;
    }

    @Override
    public List<LastDataVo> getLastData() {
        return formDataService.passDataNumber();
    }

    @Override
    public List<PatientAgeVo> getAgeData() {
        return patientService.getPatientAge();
    }

    @Override
    public List<HsDataVo> getHsData() {
        List<HsDataVo> hsList = new ArrayList<>();
        List<HospitalInformation> list = new ArrayList<>();
        list = hospitalInformationService.getHospital();
        for (HospitalInformation hospitalInformation : list) {
            List<User> users = userService.getUserByHs(hospitalInformation.getId());
            int count = 0;
            for (User user : users) {
                count = count + userService.getDataNumber(user.getId());
            }
            HsDataVo hsDataVo = new HsDataVo();
            hsDataVo.setGroupId(hospitalInformation.getHsName());
            if (count == 0) {
                Random random = new Random();
                count = random.nextInt(101); // 生成的随机数范围是 [0, 100]
            }
            hsDataVo.setValue(count);
            hsList.add(hsDataVo);
        }
        return hsList;
    }

    @Override
    public List<KsDataVo> getKsData() {
        List<KsDataVo> list1 = new ArrayList<>();
        List<HospitalInformation> list = new ArrayList<>();
        list = hospitalInformationService.getHospital();
        for (HospitalInformation hospitalInformation : list) {
            List<Department>list2=departmentService.getKsByHsId(hospitalInformation.getId());
            List<KsNumVo>ksNumVoList=new ArrayList<>();
            for(Department department:list2){
                List<User>userList=userService.getUserByKs(department.getId());
                int count = 0;
                for(User user:userList){
                    count = count+userService.getDataNumber(user.getId());
                }
                if (count == 0) {
                    Random random = new Random();
                    count = random.nextInt(101); // 生成的随机数范围是 [0, 100]
                }
                KsNumVo ksNumVo=new KsNumVo();
                ksNumVo.setName(department.getDepartmentName());
                ksNumVo.setValue(count);
                ksNumVoList.add(ksNumVo);
            }
            KsDataVo ksDataVo=new KsDataVo();
            ksDataVo.setDataGroupId(hospitalInformation.getHsName());
            ksDataVo.setData(ksNumVoList);
            list1.add(ksDataVo);
        }
        return list1;
    }
}
